package com.admissions.tylechoi;

import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.admissions.AdmissionsActivity;
import com.admissions.BaseActivity;
import com.admissions.R;
import com.admissions.adapter.TruongAdapter;
import com.admissions.common.Constants;
import com.admissions.data.DataPaser;
import com.admissions.model.TruongModel;
import com.admissions.network.HttpNetService;

public class TyLeChoi extends BaseActivity {
	private Resources mRes;
	public static TextView tvTruong;
	public static String idTruong;
	private List<TruongModel> listTruong;
	private String tyLeChoi;
	private Button btnXem;
	public static AlertDialog alertDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ty_le_choi);
		mRes = getResources();
	}

	@Override
	protected void onResume() {
		super.onResume();
		idTruong = null;
		initLayout();

		// Setup Header
		String headerContent = mRes.getString(R.string.header_ty_le_choi);
		String headerComment = mRes
				.getString(R.string.header_comment_ty_le_choi);
		Drawable icon = mRes.getDrawable(R.drawable.ic_ty_le_choi_selected);
		((AdmissionsActivity) getParent()).setupHeader(icon, headerContent,
				headerComment);

		DownloadData data = new DownloadData(this);
		data.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.chon_truong:
			LayoutInflater layoutInflater = LayoutInflater.from(this);
			View layoutList = layoutInflater.inflate(R.layout.list, null);
			ListView listItem = (ListView) layoutList
					.findViewById(R.id.listview);
			TruongAdapter truongAdapter = new TruongAdapter(this, listTruong, TYPE_TY_LE_CHOI);
			listItem.setAdapter(truongAdapter);
			alertDialog = new AlertDialog.Builder(this)
					.setIcon(R.drawable.icon).setTitle("Chọn trường")
					.setView(layoutList).create();
			alertDialog.show();

			break;

		case R.id.btn_xem:
			DownloadTyLeChoi downloadTyLeChoi = new DownloadTyLeChoi(this);
			downloadTyLeChoi.execute();

			break;
		default:
			break;
		}
	}

	public void initLayout() {
		tvTruong = (TextView) findViewById(R.id.chon_truong);
		btnXem = (Button) findViewById(R.id.btn_xem);
		tvTruong.setOnClickListener(this);
		btnXem.setOnClickListener(this);
	}

	private class DownloadData implements Constants {
		private Context context;
		private ProgressDialog dialog;

		public DownloadData(Context c) {
			context = c;
		}

		private void execute() {
			new ShowItems().execute(new Void[] { null });
		}

		public class ShowItems extends AsyncTask<Void, Void, Boolean> {
			public ShowItems() {
			}

			@Override
			protected Boolean doInBackground(Void... arg0) {
				listTruong = DataPaser
						.getListTruongCoTyLeChoi(HttpNetService
								.getJsonByURL(URL_LIST_TRUONG_CO_TY_LE_CHOI));

				return true;
			}

			@Override
			protected void onPreExecute() {
				dialog = new ProgressDialog(context);
				dialog.setCancelable(true);
				dialog.setMessage("Đang tải...");
				dialog.show();
			}

			@Override
			protected void onPostExecute(Boolean result) {
				if (dialog.isShowing() == true) {
					if (result == true) {

					} else {
						showDialogWithInvalidConnection();
					}

				}
				dialog.dismiss();
			}

		}

	}

	private class DownloadTyLeChoi implements Constants {
		private Context context;
		private ProgressDialog dialog;

		public DownloadTyLeChoi(Context c) {
			context = c;
		}

		private void execute() {
			new ShowItems().execute(new Void[] { null });
		}

		public class ShowItems extends AsyncTask<Void, Void, Boolean> {
			public ShowItems() {
			}

			@Override
			protected Boolean doInBackground(Void... arg0) {
				tyLeChoi = DataPaser.getTyLeChoi(HttpNetService
						.getJsonByURL(URL_TY_LE_CHOI + idTruong));

				return true;
			}

			@Override
			protected void onPreExecute() {
				dialog = new ProgressDialog(context);
				dialog.setCancelable(true);
				dialog.setMessage("Đang tải...");
				dialog.show();
			}

			@Override
			protected void onPostExecute(Boolean result) {
				if (dialog.isShowing() == true) {
					if (result == true) {
						LayoutInflater layoutInflater = LayoutInflater
								.from(context);
						View layoutList = layoutInflater.inflate(
								R.layout.ty_le_choi_view, null);
						WebView webView = (WebView) layoutList
								.findViewById(R.id.webview);
						webView.loadDataWithBaseURL("",
				                START + tyLeChoi.replace("\n", "<br/>")
				                    + END, "text/html", "utf-8", "");
						alertDialog = new AlertDialog.Builder(context)
								.setIcon(R.drawable.icon)
								.setTitle("Tỷ lệ chọi").setView(layoutList)
								.create();
						alertDialog.show();

					} else {
						showDialogWithInvalidConnection();
					}

				}
				dialog.dismiss();
			}

		}

	}
}
